﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace umbraco
{
    using System;
    using System.Globalization;
    using System.Web.UI.WebControls;
    using umbraco.ACSExtensions;

    public partial class ViewMemberGroupMappings : BasePages.UmbracoEnsuredPage
    {
        private MappingRuleService mappingRuleService = new MappingRuleService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                var identityProviderName = this.Request.QueryString["identityProviderName"];
                var memberGroupName = this.Request.QueryString["memberGroupName"];
                this.MainPanel.Text = "Business Identity Providers -> " + identityProviderName + " -> " + memberGroupName;

                this.BindMappings();
            }
        }

        public void BindMappings()
        {
            var identityProviderId = this.Request.QueryString["identityProviderId"];
            var identityProviderName = this.Request.QueryString["identityProviderName"];
            var memberGroupId = int.Parse(this.Request.QueryString["memberGroupId"], CultureInfo.InvariantCulture);
            var memberGroupName = this.Request.QueryString["memberGroupName"];
            var mappings = mappingRuleService.RetrieveMappingsByMemberGroup(
                    new IdentityProviderInfo { Id = identityProviderId, Name = identityProviderName },
                    new MemberGroupInfo { Id = memberGroupId, Name = memberGroupName });

            this.MappingsRepeater.DataSource = mappings;
            this.MappingsRepeater.DataBind();
        }

        public void BindMapping(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal claimType = (Literal)e.Item.FindControl("ClaimType");
                Literal claimValue = (Literal)e.Item.FindControl("ClaimValue");
                Button editButton = (Button)e.Item.FindControl("EditButton");
                Button deleteButton = (Button)e.Item.FindControl("DeleteButton");

                var mapping = (MappingRule)e.Item.DataItem;
                var identityProviderId = this.Request.QueryString["identityProviderId"];
                var identityProviderName = this.Request.QueryString["identityProviderName"];
                var memberGroupId = this.Request.QueryString["memberGroupId"];
                var memberGroupName = this.Request.QueryString["memberGroupName"];

                claimType.Text = mapping.ClaimType;
                claimValue.Text = mapping.ClaimValue;

                if (!string.IsNullOrEmpty(claimType.Text))
                {
                    editButton.CommandArgument = mapping.Id.ToString();
                    editButton.PostBackUrl = "EditMemberGroupMapping.aspx?mappingId=" + mapping.Id;
                    editButton.Text = ui.Text("edit");

                    deleteButton.CommandArgument = mapping.Id.ToString();
                    deleteButton.OnClientClick = "return confirm(\"" + ui.Text("confirmdelete") + " '" + mapping.Name + "' mapping rule ?\")";
                    deleteButton.Text = ui.Text("delete");
                }
                else
                {
                    editButton.Visible = false;
                    deleteButton.Visible = false;
                }
            }
        }

        public void DeleteMapping(object sender, CommandEventArgs e)
        {
            Guid mappingId = Guid.Empty;

            if (Guid.TryParse(e.CommandArgument.ToString(), out mappingId))
            {
                this.mappingRuleService.DeleteMapping(mappingId);
                this.BindMappings();
            }
        }
    }
}